package threecard.android;

import threecard.android.httpHelper.AccountDbHelper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Register extends MainActivity implements OnClickListener{
	private EditText mUsername;
	private EditText mPwd;
	private EditText mConfirmPwd;
	private EditText mEmail = null;
	public static final String NOTICE ="Notice";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		mUsername = (EditText) findViewById(R.id.register_username);
		mPwd = (EditText) findViewById(R.id.register_password);
		mConfirmPwd = (EditText) findViewById(R.id.register_password_retype);
		mEmail = (EditText) findViewById(R.id.register_email);
		
		Button registerButton = (Button) findViewById(R.id.register_button);
		registerButton.setOnClickListener(this);
		
    	Button cancelButton = (Button) findViewById(R.id.register_cancel_button);
    	cancelButton.setOnClickListener(this);
	}//end of onCreate method

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch(arg0.getId()){
		case R.id.register_button:
			int usernameLen = mUsername.getText().length();
			int pwdLen = mPwd.getText().length();
			int pwdLen1 = mConfirmPwd.getText().length();
			String pwd = mPwd.getText().toString();
			String conPwd = mConfirmPwd.getText().toString();
			if (usernameLen < 6 || pwdLen < 6 || pwdLen1 < 6  ){
				createDialog(NOTICE, "Username and password is at least 6 characters");
			}else if( ! isEmailAddress(mEmail.getText().toString())){
				createDialog(NOTICE, "Invalid email!");
			}
			else if ( pwd.equals(conPwd) == false){
				createDialog(NOTICE, "Password's not matched");
			}else{
			//	registerForUser(mUsername.getText().toString(),md5(mPwd.getText().toString()),mEmail.getText().toString());
				AccountDbHelper account = new AccountDbHelper();
				boolean result = 
						account.actionRegister(mUsername.getText().toString(),md5(mPwd.getText().toString()),mEmail.getText().toString());
				if (result == true){
					createDialog("Success","Your register is successfull");
					Intent hello_screen_intent = new Intent(this, Login.class);
					startActivity(hello_screen_intent);
				}else{
					createDialog(NOTICE, "This username or email is used,please choose another!");
				}
			}
			break;
		case R.id.register_cancel_button:
			setResult(RESULT_CANCELED);
			finish();
		default:
			break;
		}
	}
	
	
}//end of class Register
